package com.yx.louti.globalException.model;

import com.yx.louti.globalException.enums.HttpResultCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

@Data
@ApiModel(value = "返回结果实体类", description = "结果实体类")
public class HttpResponseResult implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "返回码")
    private Integer code;

    @ApiModelProperty(value = "返回消息")
    private String message;

    @ApiModelProperty(value = "返回数据")
    private Object data;

    @ApiModelProperty(value = "异常数据")
    private String exception;

    @ApiModelProperty(value = "时间戳")
    private long timestamp = System.currentTimeMillis();

    private HttpResponseResult() {

    }

    public HttpResponseResult(HttpResultCode HttpResultCode, Object data) {
        this.code = HttpResultCode.code();
        this.message = HttpResultCode.message();
        this.data = data;
    }

    private void setHttpResultCode(HttpResultCode HttpResultCode) {
        this.code = HttpResultCode.code();
        this.message = HttpResultCode.message();
    }

    // 返回成功
    public static HttpResponseResult success() {
        HttpResponseResult result = new HttpResponseResult();
        result.setHttpResultCode(HttpResultCode.SUCCESS);
        return result;
    }

    // 返回成功
    public static HttpResponseResult success(Object data) {
        HttpResponseResult result = new HttpResponseResult();
        result.setHttpResultCode(HttpResultCode.SUCCESS);
        result.setData(data);
        return result;
    }

    // 返回失败
    public static HttpResponseResult fail(Integer code, String message, String exception) {
        HttpResponseResult result = new HttpResponseResult();
        result.setCode(code);
        result.setMessage(message);
        result.setException(exception);
        return result;
    }

    // 返回失败
    public static HttpResponseResult fail(HttpResultCode HttpResultCode) {
        HttpResponseResult result = new HttpResponseResult();
        result.setHttpResultCode(HttpResultCode);
        return result;
    }
}
